$(function() {
	artist_list.run();
});

var artist_list = {
	run : function() {
		this._init_();
		this._start_();
		this._init_events();
	},

	_init_ : function() {
		this.scrollPage = 1;		
		this.artist = $("#artist").val();
		this.pageCount = $("#pageCount").val() || 0;			
	},

	_start_ : function() {
		//考虑大屏问题
		if($(window).height() >= $(document).height()){
			this._query_artist_musics();
		}
	},

	_init_events : function() {		
		this._bind_image_events($("#artist_musics_sub_div"));
		
		$.scroll(function(){
			artist_list._handle_scroll();
		});
	},
	
	_bind_image_events: function($div){
		$("div.mi_box > div.img", $div).hover(function() {
			$(this).children('div.tc_btn').fadeIn("slow");
		}, function() {
			$(this).children('div.tc_btn').stop(true, false).fadeOut("fast");
		});		
	},
	
	_handle_scroll: function(){
		this._query_artist_musics();
	},
	
	_query_artist_musics: function(){
		if(this.scrollPage >= this.pageCount){
			return;
		}		
		
		this.scrollPage += 1;
		var url = main.contextPath + "/music/artist/list_musics";
		var data = "artist=" + encodeURIComponent(this.artist) + "&page=" + this.scrollPage;
		var $loading = $("#loading_div");
		$.ajax(url, {
			data: data,
			beforeSend: function(){
				$loading.show();
			},
			success: function(result){
				artist_list._handle_result(result);
			},
			complete: function(){
				$loading.hide();
			}
		});		
	},
	
	_handle_result: function(result){
		var $div = $("<div>" + result + "</div>");
		this._bind_image_events($div);
		$("#artist_musics_sub_div").append($div.children());
	}
};